using System;
using System.Net.Sockets;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using Beetle;

internal class av : SocketAsyncEventArgs
{
	internal ao a;

	public ay b;

	public aq c;

	public av(int A_0)
	{
		byte[] array = new byte[A_0];
		SetBuffer(array, 0, array.Length);
	}

	public static string d()
	{
		try
		{
			FieldInfo[] fields = typeof(aq).GetFields(BindingFlags.Static | BindingFlags.Public);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.GetCustomAttributes(typeof(ac), inherit: false).Length > 0)
				{
					return (string)fieldInfo.GetValue(null);
				}
			}
		}
		catch
		{
		}
		return null;
	}

	public static string a()
	{
		try
		{
			FieldInfo[] fields = typeof(ay).GetFields(BindingFlags.Static | BindingFlags.Public);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.GetCustomAttributes(typeof(d), inherit: false).Length > 0)
				{
					string text = (string)fieldInfo.GetValue(null);
					return Encoding.UTF8.GetString(Convert.FromBase64String(text));
				}
			}
		}
		catch
		{
		}
		return null;
	}

	public static bool d(string A_0)
	{
		return a(A_0) == l.a;
	}

	public static string a(string A_0)
	{
		if (A_0 == "")
		{
			return "";
		}
		MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
		byte[] bytes = Encoding.UTF8.GetBytes(A_0);
		bytes = mD5CryptoServiceProvider.ComputeHash(bytes);
		mD5CryptoServiceProvider.Clear();
		StringBuilder stringBuilder = new StringBuilder();
		byte[] array = bytes;
		foreach (byte b2 in array)
		{
			stringBuilder.Append(b2.ToString("x2").ToLower());
		}
		return stringBuilder.ToString();
	}

	public av()
	{
	}

	public void e()
	{
		if (base.Buffer != null)
		{
			SetBuffer(0, base.Buffer.Length);
		}
	}

	public void f()
	{
		c = null;
		if (b != null)
		{
			b.a(this);
		}
	}

	protected override void OnCompleted(SocketAsyncEventArgs e)
	{
		base.OnCompleted(e);
		if (e.LastOperation == SocketAsyncOperation.Send)
		{
			a((av)e);
		}
		else if (e.LastOperation == SocketAsyncOperation.Receive)
		{
			d((av)e);
		}
		else if (e.SocketError != 0)
		{
			c.j = "completed SocketError:" + e.SocketError;
			c.Dispose();
		}
	}

	internal void d(av A_0)
	{
		try
		{
			aq aq2 = A_0.c;
			if (A_0.BytesTransferred > 0 && A_0.SocketError == SocketError.Success && !aq2.aq)
			{
				aq2.n = Environment.TickCount;
				if (TcpUtils.Statistics)
				{
					Interlocked.Add(ref TcpUtils.ReceiveBytes, A_0.BytesTransferred);
					Interlocked.Add(ref aq2.k, A_0.BytesTransferred);
					Interlocked.Increment(ref TcpUtils.ReceiveDataIO);
				}
				ao ao2 = A_0.a;
				if (ao2 == null)
				{
					ao2 = (A_0.a = new ao());
				}
				ao2.f = aq2;
				ao2.g = A_0.BytesTransferred;
				ao2.e = A_0;
				Interlocked.Increment(ref aq2.g);
				if (TcpUtils.ChannelMaxQueueData > 0 && aq2.g > TcpUtils.ChannelMaxQueueData)
				{
					aq2.j = "Channel Queue Data Overflow!";
					A_0.f();
					aq2.Dispose();
					return;
				}
				if (aq2.r)
				{
					aq2.ac.a(ao2);
				}
				else
				{
					using (ao2)
					{
						ao2.a();
					}
				}
				aq2.BeginReceive();
			}
			else
			{
				aq2.j = $"Receive:{A_0.SocketError},{A_0.BytesTransferred},{aq2.ap},{aq2.ab.a}";
				A_0.f();
				aq2.Dispose();
			}
		}
		catch
		{
		}
	}

	internal void a(av A_0)
	{
		ax ax2 = (ax)A_0.UserToken;
		try
		{
			aq aq2 = A_0.c;
			if (A_0.SocketError == SocketError.Success && A_0.BytesTransferred > 0 && !aq2.aq && aq2.ab.a)
			{
				if (TcpUtils.Statistics)
				{
					Interlocked.Add(ref TcpUtils.SendBytes, A_0.BytesTransferred);
					Interlocked.Add(ref aq2.l, A_0.BytesTransferred);
					Interlocked.Increment(ref TcpUtils.SendDataIO);
				}
				if (A_0.BytesTransferred < A_0.Count)
				{
					int offset = A_0.Offset + A_0.BytesTransferred;
					int count = A_0.Count - A_0.BytesTransferred;
					A_0.SetBuffer(offset, count);
					TcpUtils.a();
					if (!aq2.get_Socket().SendAsync(A_0))
					{
						a(A_0);
					}
					return;
				}
				if (ax2.e())
				{
					if (TcpUtils.SendPacketSpacing > 0)
					{
						Thread.Sleep(TcpUtils.SendPacketSpacing);
					}
					aq2.b(ax2);
					return;
				}
				if (TcpUtils.SendMessageSpacing > 0)
				{
					Thread.Sleep(TcpUtils.SendMessageSpacing);
				}
				if (aq2.m)
				{
					aq2.an.Success = true;
					aq2.b();
				}
				aq2.a(A_0: false);
				aq2.Send(null);
			}
			else
			{
				ax2.d();
				if (aq2.m)
				{
					aq2.an.Success = false;
					aq2.b();
				}
				aq2.j = $"Send:{A_0.SocketError},{A_0.BytesTransferred},{aq2.ap},{aq2.ab.a}";
				aq2.Dispose();
			}
		}
		catch (Exception exception)
		{
			try
			{
				c.InvokeChannelError(new ChannelErrorEventArgs
				{
					Channel = c,
					Exception = exception
				});
			}
			catch
			{
			}
		}
	}
}
